//
//  TabBarViewController.m
//  DemoTest
//
//  Created by DaShuEdu on 2017/4/25.
//  Copyright © 2017年 DaShuEdu. All rights reserved.
//

#import "TabBarViewController.h"
#import "CustomNavViewController.h"
#import "CustomTabBar.h"

@interface TabBarViewController ()<CustomTabBarProtocol>

@property (nonatomic, assign) BOOL showAdd;

@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self setValue:[[CustomTabBar alloc] initWithShowAdd:self.showAdd] forKey:@"tabBar"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (instancetype)tabBarControllerShowAddItem:(BOOL)showAddItem withAddChildVCsBlock:(void(^)(TabBarViewController *tabBarVC))addVCBlock with:addItemSize with:addItemBGImageName
//+ (instancetype)tabBarControllerShowAddItem:(BOOL)showAddItem withAddChildVCsBlock:(void(^)(TabBarViewController *tabBarVC))addVCBlock
{
    TabBarViewController *tabbarVC = [[TabBarViewController alloc] init];
    tabbarVC.showAdd = showAddItem;
    
    if (addVCBlock) {
        addVCBlock(tabbarVC);
    }
    if (addItemSize) {
        tabbarVC.addSize = addItemSize;
    }
    if (addItemBGImageName) {
        tabbarVC.addImageName = addItemBGImageName;
    }
    
    CustomTabBar *customBar = [[CustomTabBar alloc] initWithShowAdd:tabbarVC.showAdd];
    customBar.protocol = tabbarVC;
    [tabbarVC setValue:customBar forKey:@"tabBar"];
    return tabbarVC;
}

- (void)addChildVC:(UIViewController *)vc title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName
{
    vc.navigationItem.title = title;
    
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [[UIImage imageNamed:normalImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    CGFloat originImageH = CGImageGetHeight(vc.tabBarItem.image.CGImage);
    CGFloat offset = 0.5;
    if (originImageH > 49) {
        offset = 17.2f;
    }
    
    // 将系统的字体位置向上移动
    [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2.5)];
    // 将系统的图片位置向上移动
    vc.tabBarItem.imageInsets = UIEdgeInsetsMake(-offset, 0, offset, 0);
    
    CustomNavViewController *navigationVc = [[CustomNavViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:navigationVc];
}

- (NSString *)plusButtonBackgroundImageName {
    NSString *imageName = nil;
    if (self.addImageName) {
        imageName = self.addImageName();
    }
    return imageName;
}

- (CGSize)plusButtonSize {
    CGSize btnSize = CGSizeZero;
    if (self.addSize) {
        btnSize = self.addSize();
    }
    return btnSize;
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}

@end
